<?php
/*
	Version   :  1.1
	Created   :  20080917
	Modified  :  20090927
	Author    :  Pablo Ramirez
	co-Author :
	Descr     : Controlador "Depots"
	Changelog : 1.0 - Version Inicial
				1.1 - Control de Acceso
*/
class DepotsController extends AppController
{
	var $name       = 'Depots';
	var $helpers    = array('Html','Javascript','Ajax');
	var $uses		= array('Depot','Country');
	//var $scaffold;
	
	function index() {
		$this->checkSession('admin');
		$this->layout = 'maintenance';
		
		if (empty($this->data)) {
		
		$depots = $this->Depot->findAll(null,'*','dep_codigo ASC');
			foreach($depots as $depot) {
				$temp = array();
				foreach($depot as $key=>$value) {
					$temp[$key] = $value;
				}
				$depotArray[] = $temp;
			}
			$this->set('depots', $depotArray);
		} 
	}
	
	function add($id = null) {
		$this->checkSession('admin');
		$this->layout = 'ajax';
		if (!empty($this->data)){
			if ($this->Depot->save($this->data)){
				$this->redirect('depots'); 

			} else {

				$this->redirect('depots'); 
			}
		} else {
			$this->set('id', $id);
			$this->set('countries',$this->Country->generateList());
		}
	}
	
	function edit($id = null) {
		$this->checkSession('admin');
		$this->layout = 'ajax';
		$this->Depot->id = $id;
		
		if (empty($this->data)){
			$this->data = $this->Depot->read();
			$this->set('id', $id);
			$this->set('countries',$this->Country->generateList());
		}else{
			if ($this->Depot->save($this->data)){
				$this->redirect('depots'); 
				//$this->flash('Item Editado','depots');
			} else {
				$this->Session->setFlash('Error al editar el Item');
			}
		}
	}
	
	function view($id = null) {
		$this->checkSession('admin');
		$this->layout = 'ajax';
		$this->Depot->id = $id;
		
		if (empty($this->data)){
			$this->data = $this->Depot->read();
			$this->set('id', $id);
		}
	}
	
	function delete($id) {
		$this->checkSession('admin');
		if ($this->Depot->del($id)) { 
			$this->set('error', '0');
		} else {
			$this->set('error','1');
		}
		$this->redirect('depots');
	
	}
}
?>